package com.bianguo.android.functiondemo.view.activity;

import android.support.annotation.Nullable;
import android.os.Bundle;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bianguo.android.functiondemo.R;
import com.bianguo.android.functiondemo.adapter.BaseRecyclerAdapter;
import com.bianguo.android.functiondemo.adapter.ReptileAdapter;
import com.bianguo.android.functiondemo.bean.UserBean;
import com.bianguo.android.functiondemo.model.ReptileModel;
import com.bianguo.android.functiondemo.presenter.ReptilePresenter;
import com.bianguo.android.functiondemo.task.ReptileTask;
import com.bianguo.android.functiondemo.utils.MLog;
import com.bianguo.android.functiondemo.view.IReptileView;

import java.util.ArrayList;
import java.util.concurrent.Executors;

import butterknife.BindView;

public class ReptileActivity extends BaseActivity<IReptileView, ReptileModel, ReptilePresenter> implements IReptileView, BaseRecyclerAdapter.OnClickItemListener {

    @BindView(R.id.reptile_recycler)
    RecyclerView reptileRecycler;

    private ArrayList<String> imageUrls;
    private ReptileAdapter adapter;

    @Override
    public int layoutId() {
        return R.layout.activity_reptile;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        reptileRecycler.setLayoutManager(new GridLayoutManager(mContext, 2));
//        reptileRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        imageUrls = new ArrayList<>();
        adapter = new ReptileAdapter(mContext, imageUrls);
        adapter.setOnClickItemListener(this);
        reptileRecycler.setAdapter(adapter);
        ReptileTask reptileTask = new ReptileTask(adapter);
        reptileTask.executeOnExecutor(Executors.newSingleThreadExecutor(), ReptileTask.WEB_URL);
//        new LinearSnapHelper().attachToRecyclerView(reptileRecycler);
        MLog.mLog("minFlingVelocity = " + reptileRecycler.getMinFlingVelocity());
        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffUtil.Callback() {
            @Override
            public int getOldListSize() {
                return 0;
            }

            @Override
            public int getNewListSize() {
                return 0;
            }

            @Override
            public boolean areItemsTheSame(int i, int i1) {
                return false;
            }

            @Override
            public boolean areContentsTheSame(int i, int i1) {
                return false;
            }
        }, true);
        diffResult.dispatchUpdatesTo(adapter);
        mPresenter = new ReptilePresenter(this);
        mPresenter.getWebServletDemoData(1);
    }

    @Override
    public void failedView(String msg, boolean isRefresh, int tag) {
        showToast(msg);
    }

    @Override
    public void onClickItemListener(BaseRecyclerAdapter adapter, View view, int position) {
        if (0 == position) {
            mPresenter.login("123432", "123");
        } else if (1 == position) {
            mPresenter.login("15700064964", "123123");
        } else if (2 == position) {
            mPresenter.register("17826866535", "123456");
        } else if (3 == position) {
            mPresenter.register("15700064964", "123123");
        } else if (4 == position) {
            mPresenter.saveUser("我是大帅比");
        } else if (5 == position) {
            mPresenter.getUsers();
        } else if (6 == position) {
            mPresenter.getUserById(1);
        }
    }

    @Override
    public void showViewLogin(UserBean userBean) {
        showToast(userBean.getUsername());
    }
}
